package com.base.cn.platform.os.controller.manage.subject;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.manage.subject.SysSubject;
import com.base.cn.platform.os.service.manage.subject.SysSubjectBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 专业Controller
 *
 * @author s.li
 * @create 2018-03-20 15:06
 */
@RestController
@RequestMapping("/manage")
public class SysSubjectController extends BaseController {

    @Autowired
    private SysSubjectBiz sysSubjectBiz;

    /**
     * 保存专业
     * @param subject 专业数据对象
     * @return Map<String,Object>
     */
    @RequestMapping("/saveSubject")
    public Map<String,Object> saveSubject(@RequestBody SysSubject subject){
        if(StringUtils.isEmpty(subject.getSubjectName())){
            return ResultUtil.ERROR("专业/科目名称不能为空");
        }
        if(subject.getSubjectName().trim().length()>30){
            return ResultUtil.ERROR("专业/科目名限30个字以内");
        }
        if(subject.getStatus().intValue()!=1 && subject.getStatus().intValue()!=2){
            return ResultUtil.ERROR("专业/科目状态错误");
        }
        if(StringUtils.isNotEmpty(subject.getDepict()) && subject.getDepict().trim().length()>255){
            return ResultUtil.ERROR("专业/科目的描述内容限225个字以内");
        }
        subject = sysSubjectBiz.saveSubject(subject);
        return ResultUtil.SUCCESS("专业保存成功",subject);
    }

    /**
     * 通过专业ID，查询专业数据对象
     * @param subjectId 专业ID
     * @return 专业数据对象
     */
    @RequestMapping("/findSubjectById")
    public SysSubject findSubjectById(@RequestParam("subjectId") BigDecimal subjectId){
        return sysSubjectBiz.findById(subjectId);
    }

    /**
     * 专业/科目修改状态
     * @param ids 专业ID串
     * @return主Map<String,Object>
     */
    @RequestMapping("/updateSubjectStatus")
    public Map<String,Object> updateSubjectStatus(@RequestParam("subjectIds") String ids,@RequestParam("status") int status){
        sysSubjectBiz.updateSubjectStatus(ids,status);
        return ResultUtil.SUCCESS();
    }

    /**
     * 修改专业/科目父级的关联
     * @param subjectId 专业ID
     * @param parentId 新的父级ID
     * @return Map<String,object>
     */
    @RequestMapping("/updateSubjectParentId")
    public Map<String,Object> updateSubjectParentId(BigDecimal subjectId,BigDecimal parentId){
        sysSubjectBiz.updateSubjectParentId(subjectId,parentId);
        return ResultUtil.SUCCESS("专业/科目父级关联修改成功");
    }

    /**
     * 获取分层好的专业/科目数据
     * @param subject 查询条件
     * @return  List<SysSubject>
     */
    @RequestMapping("/findLayeredSubjectList")
    public List<SysSubject> findLayeredSubjectList(@RequestBody(required = false) SysSubject subject){
        return sysSubjectBiz.findLayeredSubjectList(subject);
    }

    /**
     * 获取专业Map，以父级ID为条件分组
     * @param subject 查询条件
     * @return Map<String,List<SysSubject>>
     */
    @RequestMapping("/findSubjectListMap")
    public Map<String,List<SysSubject>> findSubjectListMap(@RequestBody(required = false) SysSubject subject){
        return sysSubjectBiz.findSubjectListMap(subject);
    }

    /**
     * 查询原始专业/科目数据列表
     * @param subject 查询条件
     * @return List<SysSubject>
     */
    @RequestMapping("/findSubjectList")
    public List<SysSubject> findSubjectList(@RequestBody(required = false) SysSubject subject){
        return sysSubjectBiz.findSubjectList(subject);
    }

    /**
     * 查询专业科目列表，通过ID为KEY转成Map
     * @param subject 查询条件
     * @return Map<BigDecimal,Subject>
     */
    @RequestMapping("/findSubjectToIdMap")
    public Map<BigDecimal,SysSubject> findSubjectToIdMap(@RequestBody(required = false) SysSubject subject){
        return sysSubjectBiz.findSubjectToIdMap(subject);
    }

    /**
     * 获取管理员用户的专业列表
     * @param userId 管理员用户ID
     * @return 专业列表
     */
    @RequestMapping("/findSysUserSubjectList")
    public List<SysSubject> findSysUserSubjectList(@RequestParam("userId") BigDecimal userId){
        return sysSubjectBiz.findSysUserSubjectList(userId);
    }

    /**
     * 根据父级IDS获取科目
     * @param parentIds 管理员用户ID
     * @return 专业列表
     */
    @RequestMapping("/findSysSubSubjectList")
    public List<SysSubject> findSysSubSubjectList(@RequestParam("parentIds") String parentIds){
        SysSubject sysSubject = new SysSubject();
        sysSubject.setParentIds(parentIds);
        return sysSubjectBiz.findSubjectList(sysSubject);
    }

    /**
     * 获取要给角色设置的所有的专业，不区分状态
     * @return 分层好的专业列表
     */
    @RequestMapping("/findSetRoleAllSubjectList")
    public List<SysSubject> findSetRoleAllSubjectList(){
        return sysSubjectBiz.findSetRoleAllSubjectList();
    }

    /**
     * 获取H5首页推荐的专业
     * @return  List<SysSubject>专业列表
     */
    @RequestMapping("/findH5IndexSubjectList")
    public List<SysSubject> findH5IndexSubjectList(){
        SysSubject subject = new SysSubject();
        subject.setPlaceKey("AHTS");
        subject.setStatus(1);
        subject.setCount(7);
        subject.setParentId(BigDecimal.ZERO);
        List<SysSubject> subjectList = sysSubjectBiz.findSubjectList(subject);
        return subjectList;
    }

    /**
     * 调整排序
     * @param currentId 要操作的节点ID
     * @param currentSort 操作的节点的新排序
     * @param oldId 被替换节点的ID
     * @param oldSort 被替换节点的排序
     * @return Map<String,Object>
     */
    @RequestMapping("/updateNodeSort")
    public void updateNodeSort(@RequestParam("currentId") BigDecimal currentId,
                               @RequestParam("currentSort") int currentSort,
                               @RequestParam("oldId") BigDecimal oldId,
                               @RequestParam("oldSort") int oldSort) {
        sysSubjectBiz.updateNodeSort(currentId, currentSort, oldId, oldSort);
    }
}
